package de.hdu.pms.ho.ctrl;

import org.apache.log4j.Logger;
import org.apache.tools.ant.taskdefs.Get;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import de.hdu.pms.ho.dao.KundeDao;
import de.hdu.pms.ho.model.Kunde;

/**
 * Control class {@link KundeController}, which initializes the necessary websites for using the program.  
 * Via @RequstMapping will the websites for
 * <ul>
 * <li>editing</li>
 * <li>saving</li>
 * <li>deleting</li> and
 * <li>showing all</li>
 * </ul>
 * be started. 
 * @author Hendrik Neumann
 */
@Controller
public class KundeController {
	private static Logger log = Logger.getLogger(KundeController.class);
	private KundeDao kundeDao = null; 
	
	/*
	 * Setter Method
	 */
	public void setKundeDao(KundeDao kundeDao) {
		this.kundeDao = kundeDao;		
	}
	
	/**
	 * Allows editing a {@link Kunde} in the database with the method {@link Get}.
	 * Starts the website edit-kunde.jsp
	 * @param id 	 
	 */
	@RequestMapping(value="/EditKunde.html", method=RequestMethod.GET)
    public ModelAndView edit(@RequestParam(required=false) Integer id) {
       	log.debug("EditKunde.html");
		ModelAndView mv = new ModelAndView();
		if (id == null) {
			mv.addObject("message", "Geben Sie bitte Ihre Daten an");
    		mv.addObject(new Kunde());
    	} else { 
    		mv.addObject("message", "Hier können Sie Ihre Daten ändern");
    		mv.addObject(kundeDao.findById(id));
    	}
		mv.addObject("title", "Registrierung");
    	mv.setViewName("open/edit-kunde");
    	return mv;
	}
	
	@RequestMapping(value="/EditKunde2.html", method=RequestMethod.GET)
    public ModelAndView edit2(@RequestParam(required=false) Integer id) {
       	log.debug("EditKunde.html");
		ModelAndView mv = new ModelAndView();
		if (id == null) {
			mv.addObject("message", "Legen Sie den Kunden an");
    		mv.addObject(new Kunde());
    	} else {
    		mv.addObject("message", "Hier können die Kundendaten geändert werden");
    		mv.addObject(kundeDao.findById(id));
    	}
		mv.addObject("title", "Kunde");
    	mv.setViewName("protected/edit-kunde2");
    	return mv;
	}
	
	/**
	 * Saves a {@link Kunde} in the database with the method POST.
	 * @param kunde 
	 * @param model
	 * @return the website AlleKunden.html
	 */
	@RequestMapping(value = "/SaveKunde.html", method = RequestMethod.POST)
	public ModelAndView save(Kunde kunde, Model model) {
		kundeDao.save(kunde);
		ModelAndView mv = new ModelAndView();
		mv.addObject("message", "Geben Sie bitte Ihre Daten an");
		mv.addObject(kunde);
		mv.addObject("title", "Hier können Sie Ihre Daten einsehen und bearbeiten");
		mv.setViewName("open/edit-kunde");
		return mv;
//		model.addAttribute("statusMessageKey", "kunden.form.msg.success");
//		
//		return "redirect:NeuerKunde.html";
	}
	
	
	/*@RequestMapping(value="NeuerKunde.html", method=RequestMethod.GET)
	public ModelAndView einKunde(@RequestParam(required=false)Integer id){
		kundeDao.findById(id);
		ModelAndView mv = new ModelAndView();
		mv.addObject("title", "Meine Daten");
		mv.addObject("message", "Folgende Daten haben Sie angelegt:");
		mv.addObject("edit", true);
		mv.setViewName("open/list-einkunde");
		return mv;
		
		It doesen't work...		
	}*/
	
	@RequestMapping(value="NeuerKunde.html", method=RequestMethod.GET)
	public ModelAndView einKunde(){
		ModelAndView mv = new ModelAndView();
		mv.addObject("title", "Anmeldung erfolgreich");
		mv.addObject("message", "Herzlichen Glückwünsch, Sie haben sich erfolgreich registriert");
		mv.addObject("edit", true);
		mv.setViewName("open/list-einkunde");
		return mv;
		
	}
	
	/**
	 * Deletes a {@link Kunde} from database.
	 * @param kunde
	 * @return
	 */
	@RequestMapping(value="/LoescheKunde.html", method=RequestMethod.GET)
	public ModelAndView delete(Kunde kunde){
		try {
			kundeDao.delete(kunde);
			return new ModelAndView("redirect:AlleKunden.html");
		} catch (DataIntegrityViolationException e) {
			ModelAndView mv = new ModelAndView("protected/errordelete");
			mv.addObject("message", e.getMessage());
			return mv;
		}
	}
	
	/**
	 * Searches for all {@link Kunde} and returns them in a Collection with the method {@link Get}.
	 * With {@link Kunden2pdf} it is possible to make a print of all {@link Kunde}
	 * @return
	 */
	@RequestMapping(value="/AlleKunden.html", method=RequestMethod.GET)
	public ModelAndView findAll() {
		ModelAndView mv = new ModelAndView();
		mv.addObject("title", "Kundendaten");
		mv.addObject("message", "Folgende Kunden sind registriert:");
		mv.addObject("kunden", kundeDao.findAll());
		mv.addObject("edit", true);
		mv.setViewName("protected/list-kunden");
		return mv;
	}
	
}
